package com.bbz.core.concurrency.work3;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 同步Demo
 * 
 * @author binbin.a.zhang
 *
 */
public class SynchronizedDemo implements Runnable {
	private static int count = 0;
	//private static AtomicInteger count=new AtomicInteger(0); 使用AtomicInteger 多綫程自增也沒有问题
	public static void main(String args[]) {
		for (int i = 0; i < 10; i++) {
			Thread thread = new Thread(new SynchronizedDemo());
			thread.start();
		}
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("result: " + count);

	}

	@Override
	public void run() {
		synchronized (SynchronizedDemo.class) {
			for (int i = 0; i < 1000000; i++)
				count++;
		}

	}

}
